package FirstProject;

import java.lang.ref.SoftReference;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;

public class CardUtil {
    static Map<String,MobileCard> cards= new HashMap<>();
    static Map<String, List<ConsumeInfo>> consumeInfos=new HashMap<>();
    MobileCard m=new MobileCard();
    Random r=new Random();
    public void initScene(){
        ServicePackage sp1=new TalkPackage(58);
        ServicePackage sp2=new SuperPackage(400,40,30,78);
        ServicePackage sp3=new NetPackage(3,68);
        MobileCard card1 = new MobileCard("13900000000", "张三", "1", sp1,100);
        MobileCard card2 = new MobileCard("13900000001", "李四", "2", sp2,200);
        MobileCard card3 = new MobileCard("13900000002", "王五", "3", sp3,500);
        cards.put(card1.getCardNumber(), card1);
        cards.put(card2.getCardNumber(), card2);
        cards.put(card3.getCardNumber(), card3);
        consumeInfos.put(card1.getCardNumber(), new ArrayList<>());
        consumeInfos.put(card2.getCardNumber(), new ArrayList<>());
        consumeInfos.put(card3.getCardNumber(), new ArrayList<>());
        addConsumeInfo(card1.getCardNumber(), new ConsumeInfo(card1.getCardNumber(), "通话", 30, 0));
        addConsumeInfo(card2.getCardNumber(), new ConsumeInfo(card2.getCardNumber(), "流量", 500, 0));
        addConsumeInfo(card3.getCardNumber(), new ConsumeInfo(card3.getCardNumber(), "短信", 20, 0));
    }
    public boolean isExistCard(String name,String passWord){
        for(MobileCard card:cards.values()){
            if(m.getUserName().equals(name)&&m.getPassWord().equals(passWord)){
                return true;
            }
        }
        return false;
    }
    public boolean isExistCard(String number){
      return cards.containsKey(number);
    }
//    public String[] getNewNumbers(int count){
//
//    }
    public void addCard(MobileCard card){
        cards.put(card.getCardNumber(),card);
        consumeInfos.put(card.getCardNumber(),new ArrayList<>());
    }
    public void delCard(String number){
        cards.remove(number);
        consumeInfos.remove(number);
    }
    public void showRemainDetail(String number) {
        MobileCard card = cards.get(number);
        if (card != null) {
            System.out.println("卡号：" + number);
            System.out.println("用户名：" + card.getUserName());
            ServicePackage pack = card.getSerPackage();
            if (pack instanceof TalkPackage) {
                TalkPackage tp = (TalkPackage) pack;
                System.out.println("剩余通话时长:" + tp.getTalkTime() + "分钟");
                System.out.println("剩余短信条数:" + tp.getSmsCount() + "条");
                if (pack instanceof SuperPackage) {
                    SuperPackage sp = (SuperPackage) pack;
                    System.out.println("剩余上网流量:"+sp.getFlow()+"MB");
                }
            } else if (pack instanceof NetPackage) {
                NetPackage np = (NetPackage) pack;
                System.out.println("剩余上网流量:"+np.getFlow()+"MB");
            }
        } else {
            System.out.println("卡号不存在！");
        }
    }
    public void showAmountDetail(String number) {
        MobileCard card = cards.get(number);
        if (card != null) {
            DecimalFormat df = new DecimalFormat("#.0");//显示一位小数
            System.out.println("卡号:"+card.getCardNumber());
            System.out.println("用户名:"+card.getUserName());
            System.out.println("套餐月资费:"+card.getSerPackage().getPrice()+"元");
            System.out.println("合计消费:"+df.format(card.getSerPackage().getPrice()));
            System.out.println("账户余额:"+card.getMoney() + "元");
        } else {
            System.out.println("卡号不存在！");
        }
    }
//添加用户的消费记录
    public void addConsumeInfo(String number,ConsumeInfo info){
        if(!cards.containsKey(number)){
            System.out.println("您输入的卡号不存在！");
            return;
        }else {
            List<ConsumeInfo> Infos = consumeInfos.get(number);
            Infos.add(info);
        }
    }
    public void useSoso(String number) {
        MobileCard card = cards.get(number);
        if (card != null) {
            // 定义6个消费场景
            String[] scenes = {//通过数组的下标可以获取不同场景
                    "问候客户，谁知其如此难缠",
                    "通知朋友手机换号",
                    "参与环境保护实施方案问卷调查",
                    "观看在线视频",
                    "下载大型文件",
                    "视频会议"
            };
            // 随机选择一个场景
            int sceneIndex=r.nextInt(scenes.length);//选场景
            System.out.println("\n=== 消费场景 ===");
            System.out.println(scenes[sceneIndex]);
            switch (sceneIndex) {
                case 0: // 通话90分钟
                    if (card.getSerPackage() instanceof CallService) {
                        CallService cs=(CallService) card.getSerPackage();
                        int extra=cs.call(90,card);//超出的
                        if (extra>0) {
                            System.out.println("通话90分钟,超出套餐"+extra + "分钟");
                        } else {
                            System.out.println("通话90分钟");
                        }
                        addConsumeInfo(number, new ConsumeInfo(number, "通话", 90, extra * 0.2));
                    }else {
                        System.out.println("当前套餐不支持通话功能！");
                    }
                    break;
                case 1: // 发送短信50条
                    if (card.getSerPackage() instanceof SendService) {
                        SendService ss=(SendService)card.getSerPackage();
                        int extra=ss.send(50, card);
                        if (extra>0) {
                            System.out.println("发送短信50条,超出套餐"+extra+"条");
                        } else {
                            System.out.println("发送短信50条");
                        }
                        addConsumeInfo(number, new ConsumeInfo(number, "短信", 50, extra * 0.1));
                    }else {
                        System.out.println("当前套餐不支持短信功能！");
                    }
                    break;
                case 2: // 发送短信5条
                    if (card.getSerPackage() instanceof SendService) {
                        SendService ss = (SendService) card.getSerPackage();
                        int extra = ss.send(5, card);
                        if (extra > 0) {
                            System.out.println("发送短信5条，超出套餐" + extra + "条");
                        } else {
                            System.out.println("发送短信5条");
                        }
                        addConsumeInfo(number, new ConsumeInfo(number, "短信", 5, extra * 0.1));
                    }else {
                        System.out.println("当前套餐不支持短信功能！");
                    }
                    break;
                case 3: // 使用流量300MB
                    if (card.getSerPackage() instanceof NetService) {
                        NetService ns = (NetService) card.getSerPackage();
                        int extra = ns.netPlay(300, card);
                        if (extra > 0) {
                            System.out.println("使用流量300MB，超出套餐"+extra+"MB");
                        } else {
                            System.out.println("使用流量300MB");
                        }
                        addConsumeInfo(number, new ConsumeInfo(number, "上网", 300, extra*0.1));
                    }else {
                        System.out.println("当前套餐不支持上网功能！");
                    }
                    break;
                case 4: // 使用流量800MB
                    if (card.getSerPackage() instanceof NetService) {
                        NetService ns = (NetService) card.getSerPackage();
                        int extra = ns.netPlay(800, card);
                        if (extra > 0) {
                            System.out.println("使用流量800MB，超出套餐" + extra + "MB");
                        } else {
                            System.out.println("使用流量800MB");
                        }
                        addConsumeInfo(number, new ConsumeInfo(number, "上网", 800, extra * 0.1));
                    }else {
                        System.out.println("当前套餐不支持上网功能！");
                    }
                    break;
                case 5: // 使用流量150MB
                    if (card.getSerPackage() instanceof NetService) {
                        NetService ns = (NetService) card.getSerPackage();
                        int extra = ns.netPlay(150, card);
                        if (extra > 0) {
                            System.out.println("使用流量150MB，超出套餐" + extra + "MB");
                        } else {
                            System.out.println("使用流量150MB");
                        }
                        addConsumeInfo(number, new ConsumeInfo(number, "上网", 150, extra * 0.1));
                    } else {
                        System.out.println("当前套餐不支持上网功能！");
                    }
                    break;
            }
        } else {
            System.out.println("卡号不存在！");
        }
    }
    public void showDescription(){
        System.out.println("===== 套餐说明 =====");
        System.out.println("1. 话痨套餐：通话500分钟/月，短信30条/月");
        System.out.println("2. 超人套餐：通话200分钟/月，短信50条/月，上网流量1GB/月");
        System.out.println("3. 网虫套餐：上网流量3GB/月");
    }
    public void changingPack(String name,String packNum){
        MobileCard card=cards.get(m.cardNumber);
        if(card!=null){
            switch (packNum){
                case "1":
                   card.setSerPackage(new TalkPackage(58));
                    System.out.println("已经更换为话痨套餐！");
                    break;
                case "2":
                    card.setSerPackage(new SuperPackage(78));
                    System.out.println("已经更换为超人套餐！");
                    break;
                case "3":
                    card.setSerPackage(new NetPackage(68));
                    System.out.println("已经更换为网虫套餐！");
            }
        }
    }
    public void printConsumeInfo(String number) {
        if (!consumeInfos.containsKey(number)) {
            System.out.println("卡号 " + number + " 不存在或无消费记录");
            return;
        }
        List<ConsumeInfo> infos = consumeInfos.get(number);//获取消费记录
        System.out.println("===== 卡号 "+number+" 的消费记录 =====");
        if (infos.isEmpty()) {
            System.out.println("（无消费记录）");
        } else {
            infos.forEach(System.out::println); //依赖ConsumeInfo的toString()
            //遍历并打印每条消费记录 可以很好的减少代码的冗余
        }
    }
    public void chargeMoney(String number,double money){
        if(!cards.containsKey(number)){
            System.out.println("您输入的卡号不存在！");
            return;
        }if(money<=0){
            System.out.println("请输入正确的金额!");
            return;
        }
        MobileCard card=cards.get(number);
        //获取到号码后 将钱用set加上充值的钱 变为当前余额
        card.setMoney(card.getMoney()+money);
    }

}
